package com.css.fxfzmh.base.attachment.service.impl;

import com.css.fxfzmh.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzmh.base.attachment.repository.AttachmentInfoRepository;
import com.css.fxfzmh.base.attachment.service.AttachmentInfoService;
import com.css.fxfzmh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2022年08月02日  15:43
 */
@Service
public class AttachmentInfoServiceImpl implements AttachmentInfoService {

    @Autowired
    private AttachmentInfoRepository attachmentInfoRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(List<AttachmentInfoEntity> attachmentInfoEntities) {
           int count = 0;
           for (AttachmentInfoEntity attachmentInfoEntity : attachmentInfoEntities){
               int row = attachmentInfoRepository.save(attachmentInfoEntity);
               count+=row;
           }
           if(count == attachmentInfoEntities.size()){
               return 1;
           }
           return 0;
    }

    @Override
    public AttachmentInfoEntity getAttach(String attachId) {
        return attachmentInfoRepository.getAttach(attachId);
    }

    @Override
    public boolean deleteFile(Map<String,String> map) {
        boolean res = false;
        String attachIds = map.get("attachId");
        for(String attachId : attachIds.split(",")){
            res = attachmentInfoRepository.deleteFile(attachId);
        }
        return res;
    }

    @Override
    public List<AttachmentInfoEntity> queryFile(String fileIds) {
        ArrayList<AttachmentInfoEntity> list = new ArrayList<>();
        if(!PlatformObjectUtils.isEmpty(fileIds)){
            for (String fileId: fileIds.split(",")) {
                AttachmentInfoEntity attachmentInfoEntity = attachmentInfoRepository.queryFile(fileId);
                if(attachmentInfoEntity != null){
                    list.add(attachmentInfoEntity);
                }
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AttachmentInfoEntity attach) {
        attachmentInfoRepository.update(attach);
    }
}
